package com.service.Impl;

import com.Dao.UserDao;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.entity.User;
import com.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserDao userDao;

    @Override
    public PageInfo<User> findAll(String name,int pageNum) {
        User user=new User();
        user.setUsername(name);
        //设置当前页，页面容量
        PageHelper.startPage(pageNum,5);
        List<User> list=userDao.findAll(user);
        PageInfo<User> pageInfo=new PageInfo<User>(list);
        if(pageInfo.getList().size()>0)
        {
            return pageInfo;
        }else{
            return null;
        }
    }

    @Override
    public int save(User user) {
        return userDao.save(user);
    }

    @Override
    public Integer delete(int id) {
        return userDao.delete(id);
    }


    @Override
    public User get(int id) {
        return userDao.get(id);
    }

    @Override
    public void update(User user) {
        userDao.update(user);
    }

    @Override
    public User selectUserById(int id){
        return userDao.selectUserById(id);
    }

    @Override
    public boolean login(User user){
        String name = user.getUsername();
        String password = user.getPassword();
        User u1 =  userDao.selectUserByName(name);
        if(u1==null){
            return false;
        }else{
            if(u1.getPassword().equals(password)){
                return true;
            }else{
                return false;
            }
        }
    }

    @Override
    public boolean register(User user){
        int x = userDao.insertUser(user);
        if(x != 0){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public User selectUserByName(String name){
        return userDao.selectUserByName(name);
    }
}

